package com.blast.cn.ui

import android.content.Intent
import android.text.TextUtils
import android.view.KeyEvent
import com.blast.cn.R
import com.blast.cn.persenter.splash.ApplyPermissionPresenter
import com.blast.cn.persenter.splash.CheckNetPresenter
import com.blast.cn.persenter.splash.LoadDetonatorInfoPresenter
import com.blast.cn.persenter.splash.TimeSettingsPresenter
import com.wxl.common.AppContext
import com.wxl.common.base.AbsActivity
import com.wxl.common.base.Works
import com.wxl.common.serialport.CJQUpdateModel
import com.wxl.common.util.ImgLoader
import kotlinx.android.synthetic.main.activity_splash_layout.*

class SplashActivity : AbsActivity() {

    private var mWorks: Works? = null

    override fun getCreateContentViewId(): Int {
        return R.layout.activity_splash_layout
    }

    override fun onCreateViewChanged() {
        setTopTitle("开机引导")
        ImgLoader.loadResouce(splashCompanyImageView, R.mipmap.icon_company)
        AppContext.appContext.saveIsOpenInputDelayData(true)//默认开启延时写入模式
        splashStartBtn.setOnClickListener {
            if (TextUtils.equals("完　成", splashStartBtn.text)) {
                startActivity(Intent(this, CheckDeviceActivity::class.java))
                finish()
            } else {
                startCheck()
            }
        }
    }

    override fun onKeyDownEvent(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_F1 || keyCode == KeyEvent.KEYCODE_BACK) {

        } else if (keyCode == KeyEvent.KEYCODE_F4) {
            if (splashStartBtn.isShown) {
                splashStartBtn.performClick()
            }
        }
        return true
    }


    private fun startCheck() {
        mWorks = Works.Builder(this)
            .add(ApplyPermissionPresenter::class.java, splashLayout)
            .add(CheckNetPresenter::class.java, splashLayout)
            .add(TimeSettingsPresenter::class.java, splashLayout)
            .add(LoadDetonatorInfoPresenter::class.java, splashLayout)
            .queue()
    }


    override fun onDestroy() {
        super.onDestroy()
        mWorks?.releaseWorks()
        CJQUpdateModel.release()
    }
}